package com.cardbaobao.cardbabyclient.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.TextView;

/**
 * 逐字显示TextView
 * Created by zad
 * 2017/3/1 14:30
 */

public class ChangeTextView extends TextView {
    /**
     * 动画开始前显示的文本
     */
    private String mFirstText;
    /**
     * 动画开始后显示的文本
     */
    private String mAfterText;

    private char[] mCharArrays;

    private int mIndex = 0;
    /**
     * 控件自身宽度
     */
    private int mWidth;
    /**
     * 控件用户设置的左内边距
     */
    private int mPaddingLeft;
    /**
     * 动画文本需要的最大宽度
     */
    private float mChangeTextMaxWidth;

    private int mGravity;

    public ChangeTextView(Context context) {
        this(context, null);
    }

    public ChangeTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ChangeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (mCharArrays != null && mCharArrays.length > 0) {
                if (mIndex >= mCharArrays.length) {
                    mIndex = -1;
                    setText(mAfterText);
                }else{
                    String mSubStr = String.valueOf(mCharArrays[mIndex]);
                    append(mSubStr);
                }
                mIndex++;
                handler.sendEmptyMessageDelayed(0, 500);
            }
        }
    };

    /**
     * 设置需要逐字变化的文本
     *
     * @param changeTxt
     */
    public void setChangeText(String changeTxt) {
        if (!TextUtils.isEmpty(changeTxt)) {
            mCharArrays = changeTxt.toCharArray();
        }
    }

    /**
     * @param text 动画开始后要显示的文本内容
     */
    public void startShowChangeTxt(String text) {
        mAfterText = mFirstText = getText().toString().trim();//默认显示的文本
        if (!TextUtils.isEmpty(text)) {
            mAfterText = text;//动画开始后显示的文本
        }
        setText(mAfterText);
        setTextPosition();
        mIndex = 0;
        handler.sendEmptyMessageDelayed(0, 500);
        setEnabled(false);
    }

    /**
     * 设置文本显示位置
     */
    private void setTextPosition(){
        mGravity = getGravity();
        mPaddingLeft = getPaddingLeft();
        if(mGravity == Gravity.CENTER){
            //只考虑文本居中情况
            setGravity(Gravity.CENTER_VERTICAL);//动画开始设置左对齐
            mChangeTextMaxWidth = getPaint().measureText(getText().toString());
            setPadding((int) ((mWidth - mChangeTextMaxWidth) / 2),getPaddingTop(),getPaddingRight(),getPaddingBottom());
        }
    }

    /**
     * 停止动画
     */
    public void stopShowChangeTxt() {
        setText(mFirstText);
        setEnabled(true);
        handler.removeMessages(0);//移除消息
        if(mGravity == Gravity.CENTER){
            //只考虑文本居中情况
            setGravity(mGravity);//归位
            setPadding(mPaddingLeft,getPaddingTop(),getPaddingRight(),getPaddingBottom());
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
    }
}
